package com.teamwings.util;

import com.teamwings.common.aspect.FormControlMap;
import com.teamwings.common.exception.ServiceException;
import com.teamwings.ctrls.FormComponent;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * 枚举控件
 */
public class FieldsLoopUtil {

    public static void loop(List<FormComponent> formComponents, Consumer<FormComponent> comsumer) {
        // 获取所有的key
        Set<String> layoutTypeList = FormControlMap.layoutTypeClassMap.keySet();
        for (FormComponent formComponent : formComponents) {
            if (formComponent == null) {
                System.out.println("查找控件对象为空。");
            }
            String type = formComponent.getType();
            // 控件类型判断 【控件类型分为：普通控件，布局控件。 他们利用自定以注解@FormControl的isLayout属性进行区分】
            if (layoutTypeList.contains(type)) {
                String layoutType = FormControlMap.layoutTypeClassMap.get(type);
                // 实例化布局控件
                //FormComponent layoutFormComponent = getFormComponentByClassname(formComponent, layoutType);
                // 获取布局里面的普通控件列表
                //List<FormComponent> componentsInTheLayout = layoutFormComponent.getFormComponentList();
                List<FormComponent> componentsInTheLayout = formComponent.getFormComponentList();

                loop(componentsInTheLayout, comsumer);
//                for (FormComponent componentInTheLayout : componentsInTheLayout) {
//                    // 处理布局控件中的普通控件
//                    doEditComponent(tableName, fieldsList, originFields, componentInTheLayout, componentTypeInTheLayout);
//                }
            } else {
                // 处理普通控件
                String className = FormControlMap.typeClassMap.get(type);
                if (StringUtils.isNotEmpty(className)) {
                    //FormComponent realComponent = getFormComponentByClassname(formComponent, className);
                    //comsumer.accept(realComponent);
                    comsumer.accept(formComponent);
                }
            }
        }
    }

    /**
     * 递归某个控件，根据Predicate，返回控件列表
     */
    public static List<FormComponent> getComponents(
            FormComponent formComponent,
            Predicate<FormComponent> condition
    ) {
        ArrayList<FormComponent> formComponents = new ArrayList<>();
        formComponents.add(formComponent);
        return getComponents(formComponents, condition, null);
    }

    /**
     * 根据Predicate条件，递归返回List<FormComponent>
     */
    public static List<FormComponent> getComponents(
            List<FormComponent> formComponents,
            Predicate<FormComponent> condition
    ) {
        return getComponents(formComponents, condition, null);
    }

    public static List<FormComponent> getComponents(
            List<FormComponent> formComponents,
            Predicate<FormComponent> condition,
            List<FormComponent> res
    ) {
        // 获取所有的key
        Set<String> layoutTypeList = FormControlMap.layoutTypeClassMap.keySet();

        if (res == null) {
            res = new ArrayList<>();
        }

        for (FormComponent formComponent : formComponents) {
            if (formComponent == null) {
                System.out.println("查找控件对象为空。");
            }
            String type = formComponent.getType();
            if (StringUtils.isEmpty(type)) {
                throw new ServiceException("控件类型不能为空!");
            }
            // 控件类型判断 【控件类型分为：普通控件，布局控件。 他们利用自定以注解@FormControl的isLayout属性进行区分】
            if (layoutTypeList.contains(type)) {
                // 如果是布局控件
                List<FormComponent> componentsInTheLayout = formComponent.getFormComponentList();
                // 判断布局控件是否符合条件
                if (condition.test(formComponent)) {
                    res.add(formComponent);
                }
                getComponents(componentsInTheLayout, condition, res);
            } else {
                // 如果是一般控件
                String className = FormControlMap.typeClassMap.get(type);
                if (className == null) {
                    // 不生成数据库字段控件
                    className = FormControlMap.noFieldClassMap.get(type);
                }

                // 判断一般控件或不生成数据库控件是否符合条件
                if (StringUtils.isNotEmpty(className)) {
                    if (condition.test(formComponent)) {
                        res.add(formComponent);
                    }
                }
            }
        }

        return res;
    }
}
